package com.test.shop.servlet;

import com.test.shop.pojo.ManageResult;
import com.test.shop.service.UserService;
import com.test.shop.service.impl.UserServiceImpl;
import com.test.shop.util.AccountUtil;
import com.test.shop.util.ShopConstant;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 徒有琴
 */
@WebServlet("/user")
public class UserServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String act = request.getParameter("act");
        if ("login".equals(act)) {
            login(request, response);
        } else if ("regist".equals(act)) {
            regist(request, response);
        }
    }

    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String mobile = request.getParameter("mobile");
        String password = request.getParameter("password");
        //验证手机号和密码，略
        ManageResult result = userService.doLogin(mobile, password);
        if (result.isSuccess()) {//登陆成功
            HttpSession session = request.getSession();
            //把登陆的用户存入到session中
            session.setAttribute(ShopConstant.SESSION_USER, result.getOther());
            request.getRequestDispatcher("/page/index.jsp").forward(request, response);
        } else {
            request.setAttribute("message", result.getMessage());
            request.getRequestDispatcher("/page/login.jsp").forward(request, response);
            return;//不要忘记写
        }

    }

    protected void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String mobile = request.getParameter("mobile");
        String password = request.getParameter("password");
        if (!AccountUtil.checkMobile(mobile)) {//非法手机号
            request.setAttribute("message", "请输入合法手机号");
            request.getRequestDispatcher("/page/regist.jsp").forward(request, response);
            return;//不要忘记写
        }
        //密码验证略，作业
        ManageResult result = userService.doRegist(mobile, password);
        if (result.isSuccess()) {
            //request.getRequestDispatcher("/page/login.jsp").forward(request, response);
            response.sendRedirect("/page/login.jsp");
        } else {
            request.setAttribute("message", result.getMessage());
            request.getRequestDispatcher("/page/regist.jsp").forward(request, response);
            //response.sendRedirect("/page/regist.jsp");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
